package com.shanghaionstar.nav.presenter;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;

import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.common.util.DeviceUtil;
import com.onstar.cn.framework.mvp.base.IMVPScheduler;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.map.OMapView;
import com.onstar.cn.map.common.DriveMode;
import com.onstar.cn.map.common.PoiItemInfo;
import com.shanghaionstar.enumtype.FunctionNameFindId;
import com.shanghaionstar.nav.NavRoutePlanDetailFragment;
import com.shanghaionstar.nav.NavRoutePlanResultFragment;
import com.shanghaionstar.nav.iview.INavRoutePlanResultView;
import com.shanghaionstar.nav.service.AbstractMapActivity;
import com.shanghaionstar.utils.BusinessUtil;
import com.shanghaionstar.utils.MapHandler;

/**
 * Created by 6335yz on 2016/7/7.
 */
public class NavRoutePlanResultPresenter extends BasePresenter implements MapHandler.OnRouteCalListener {
    private AbstractMapActivity mapActivity;
    private String timeInfo;
    private String disInfo;
    //    private MyBroadCast myBroadCast;
    private PoiItemInfo routeEndPoiItemInfo, routeStartPoiItemInfo;
    private MapHandler mapHandler;
    private FragmentManager fragmentManager;

    public NavRoutePlanResultPresenter(IMVPScheduler mvpScheduler, AbstractMapActivity mapActivity, MapHandler mapHandler) {
        super(mvpScheduler);
        this.mapActivity = mapActivity;
        this.mapHandler = mapHandler;
        mapHandler.registOnRouteCalListener(this);
        fragmentManager = mapActivity.getSupportFragmentManager();
    }

    public void getData(Bundle bun) {
        if (bun != null) {
            routeStartPoiItemInfo = (PoiItemInfo) bun.getSerializable("routeStartPoiItemInfo");
            routeEndPoiItemInfo = (PoiItemInfo) bun.getSerializable("routeEndPoiItemInfo");
//            mapHandler.caculateRoute(routeStartPoiItemInfo, routeEndPoiItemInfo);
            mapHandler.caculateRoute(routeStartPoiItemInfo, routeEndPoiItemInfo);
            mapHandler.initMapView(10, routeStartPoiItemInfo);
        }
    }

    public void getDriveModeResult(int driverMode) {
        if (routeStartPoiItemInfo != null && routeEndPoiItemInfo != null) {
            mapHandler.caculateRouteByMode(routeStartPoiItemInfo, routeEndPoiItemInfo, driverMode);
            mapHandler.initMapView(10, routeStartPoiItemInfo);
        }
    }

    @Override
    public void onRouteComplete() {
        showRoute();
    }

    @Override
    public void onRouteError() {

    }

    public void detailClick() {
        BusinessUtil.sendReportByClickType(mapActivity,
                FunctionNameFindId.Routeresults_detail.getStringValue());
        NavRoutePlanDetailFragment navRoutePlanDetailFragment = NavRoutePlanDetailFragment.newInstance(routeStartPoiItemInfo,
                routeEndPoiItemInfo, timeInfo, disInfo);
        mapActivity.addFragment(fragmentManager, navRoutePlanDetailFragment, true);
        mapActivity.showFragment(fragmentManager, NavRoutePlanDetailFragment.class, NavRoutePlanResultFragment.class);
    }


    public void showRoute() {
//        PoiLocationInfoInstance.getInstance().setRouteSearchStatus(RouteSearchStatusType.Completed);
        updateRouteTip();
        if (mapHandler != null) {
            mapHandler.showRoute();
        }
    }


    private void updateRouteTip() {
        String dis = "", time = "";
        dis = mapHandler.getRouteDistance();
        time = mapHandler.getRouteTime();
        if (DeviceUtil.getLanguage().equals("en")) {
            if (!CommonUtil.isNull(time)) {
                time = time.replace("分钟", "m");
                time = time.replace("小时", "h");
            }
            if (!CommonUtil.isNull(dis)) {
                dis = dis.replace("公里", "km");
                dis = dis.replace("米", "m");
            }
        }
        timeInfo = time;
        disInfo = dis;
        if (view instanceof INavRoutePlanResultView) {
            ((INavRoutePlanResultView) view).setTxt(dis, time);
        }
        //距离
//        spaceTv.setText(dis);
//        //时间
//        timeTv.setText(time);
    }


//    class MyBroadCast extends BroadcastReceiver {
//
//        @Override
//        public void onReceive(Context context, Intent intent) {
//            if (intent.hasExtra("SEARCHROUTE")) {
//                String flag = intent.getStringExtra("SEARCHROUTE");
//                if ("success".equals(flag)) {
//                    showRoute();
//                }
//            }
//        }
//    }

    public void onResume(OMapView mapView) {
//        myBroadCast = new MyBroadCast();
//        mContext.registerReceiver(myBroadCast, new IntentFilter("SearchRouteSuccess"));
        mapView.onResume();
    }

    public void onDestroy(OMapView mapView) {
        mapHandler = null;
    }

    public void onStop() {
//        mContext.unregisterReceiver(myBroadCast);
    }


}
